using System;
using System.Collections.ObjectModel;
using FireStarter.Base;
using FireStarterModeller.BO;
using Habanero.Base.Exceptions;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using NUnit.Framework;
 

namespace FireStarterModeller.Test.BO
{
    /// <summary>
    /// Provides a place to write custom tests for UIFormInfo objects.
    /// This file is only written once and can be changed.  The Def file
    /// attached to this as a dependent is rewritten with each regeneration
    /// and contains the standard tests for UIFormInfo.
    /// Regenerate this test project whenever there have been changes to the
    /// business objects.
    /// If tests are failing due to a unique setup in your application,
    /// you can either override the Create methods in TestUtils, or you
    /// can add the test to the ignore list below and reimplement it here.
    /// </summary>
    public partial class TestUIFormInfo
    {
        [SetUp]
        public override void SetupTest()
        {
            //Runs every time that any testmethod is executed
            base.SetupTest();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //------------------------------------------------------------
            // Use this list to ignore generated tests that are failing
            // due to a unique condition in your application.
            // Remember to reimplement the test here.
            //------------------------------------------------------------
            //_ignoreList.Add("TestMethodName", "Reason for ignoring it");
            base.SetupFixture();
        }

        [Test]
        public void Test_ToString_WhenViewNull_ShouldReturnFormID()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsUIFormInfo.CreateUnsavedValidUIFormInfo_WNoUIView();
            //---------------Assert Precondition----------------
            Assert.IsNull(uiFormInfo.UIView);
            //---------------Execute Test ----------------------
            var toString = uiFormInfo.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(uiFormInfo.UIFormID.ToString(), toString);
        }
        [Test]
        public void Test_ToString_WhenViewNotNull_ShouldReturnFormForViewName()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsUIFormInfo.CreateUnsavedValidUIFormInfo();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(uiFormInfo.UIView);
            //---------------Execute Test ----------------------
            var toString = uiFormInfo.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(uiFormInfo.UIView.ViewName + "_FormInfo", toString);
        }

        [Test]
        public void Test_DeleteUIFormInfo_MarksAsDeleted()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsShared.CreateSavedUIFormInfo();

            //---------------Assert Precondition----------------
            Assert.IsFalse(uiFormInfo.Status.IsNew);
            string message;
            Assert.IsTrue(uiFormInfo.IsDeletable(out message));

            //---------------Execute Test ----------------------
            uiFormInfo.MarkForDelete();
            uiFormInfo.Save();

            //---------------Test Result -----------------------  
            Assert.IsTrue(uiFormInfo.Status.IsNew);
            Assert.IsTrue(uiFormInfo.Status.IsDeleted);
        }

        //[Test]
        //public void Test_InsertNewUIFormInfo_Invalid()
        //{
        //    //---------------Set up test pack-------------------
        //    UIFormInfo uiFormInfo = new UIFormInfo();

        //    //---------------Assert Precondition----------------
        //    Assert.IsTrue(uiFormInfo.Status.IsNew);

        //    //---------------Execute Test ----------------------
        //    try
        //    {
        //        uiFormInfo.Width = null;
        //        uiFormInfo.Height = null;
        //        uiFormInfo.Save();
        //        Assert.Fail("expected Err");
        //    }
        //    //---------------Test Result -----------------------
        //    catch (BusObjectInAnInvalidStateException ex)
        //    {
        //        StringAssert.Contains("Height' is a compulsory field and has no value", ex.Message);
        //        StringAssert.Contains("Width' is a compulsory field and has no value", ex.Message);
        //    }
        //    Assert.IsTrue(uiFormInfo.Status.IsNew);
        //}

        [Test]
        public void Test_InsertNewUIFormInfo_DefaultValues()
        {
            //---------------Set up test pack-------------------

            //---------------Execute Test ----------------------
            UIFormInfo uiFormInfo = new UIFormInfo();

            //---------------Test Result -----------------------
            Assert.AreEqual(300, uiFormInfo.Width);
            Assert.AreEqual(250, uiFormInfo.Height);
        }

        [Test]
        public void Test_InsertNewUIFormInfo()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsShared.CreateUnsavedValidUIFormInfo();

            //---------------Assert Precondition----------------
            Assert.IsTrue(uiFormInfo.Status.IsNew);

            //---------------Execute Test ----------------------
            uiFormInfo.Save();

            //---------------Test Result -----------------------
            Assert.IsFalse(uiFormInfo.Status.IsNew);
        }


        [Test]
        public void Test_DeleteUIFormInfo_DeletesTabInfo()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsShared.CreateSavedUIFormInfo();
            UITab uiTabInfo1 = TestUtilsShared.CreateSavedUITabInfo(uiFormInfo);
            UITab uiTabInfo2 = TestUtilsShared.CreateSavedUITabInfo(uiFormInfo);
            //---------------Assert Precondition----------------
            Assert.IsFalse(uiFormInfo.Status.IsNew);
            string message;
            Assert.IsTrue(uiFormInfo.IsEditable(out message));

            //---------------Execute Test ----------------------
            uiFormInfo.MarkForDelete();
            uiFormInfo.Save();

            //---------------Test Result -----------------------
            Assert.IsTrue(uiFormInfo.Status.IsNew);
            Assert.IsTrue(uiFormInfo.Status.IsDeleted);
            Assert.IsTrue(uiTabInfo1.Status.IsDeleted);
            Assert.IsTrue(uiTabInfo2.Status.IsDeleted);
        }

        [Test]
        public void Test_DeleteUIFormInfo_DeletesColumnLayout()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsShared.CreateSavedUIFormInfo();
            UITab uiTab = TestUtilsShared.CreateSavedUITabInfo(uiFormInfo);
            UIColumnLayout uiColumnLayoutInfo1 = TestUtilsShared.CreateSavedUIColumnLayoutInfo(uiTab);
            UIColumnLayout uiColumnLayoutInfo2 = TestUtilsShared.CreateSavedUIColumnLayoutInfo(uiTab);
            //---------------Assert Precondition----------------
            Assert.IsFalse(uiFormInfo.Status.IsNew);
            string message;
            Assert.IsTrue(uiFormInfo.IsEditable(out message));

            //---------------Execute Test ----------------------
            uiFormInfo.MarkForDelete();
            uiFormInfo.Save();

            //---------------Test Result -----------------------
            Assert.IsTrue(uiFormInfo.Status.IsNew);
            Assert.IsTrue(uiFormInfo.Status.IsDeleted);
            Assert.IsTrue(uiTab.Status.IsDeleted);
            Assert.IsTrue(uiColumnLayoutInfo1.Status.IsDeleted);
            Assert.IsTrue(uiColumnLayoutInfo2.Status.IsDeleted);
        }

        [Test]
        public void Test_DeleteUIFormInfo_DeletesFieldInfo()
        {
            //---------------Set up test pack-------------------
            BusinessObjectManager.Instance.ClearLoadedObjects();

            UIFormInfo uiFormInfo = TestUtilsShared.CreateSavedUIFormInfo();
            UITab uiTab = TestUtilsShared.CreateSavedUITabInfo(uiFormInfo);
            UIColumnLayout uiColumnLayout = TestUtilsShared.CreateSavedUIColumnLayoutInfo(uiTab);

            UIField uiFieldInfo1 = TestUtilsShared.CreateSavedUIField(uiColumnLayout);
            UIField uiFieldInfo2 = TestUtilsShared.CreateSavedUIField(uiColumnLayout);
            //---------------Assert Precondition----------------
            Assert.IsFalse(uiFormInfo.Status.IsNew);
            string message;
            Assert.IsTrue(uiFormInfo.IsEditable(out message));

            //---------------Execute Test ----------------------
            uiFormInfo.MarkForDelete();
            uiFormInfo.Save();

            //---------------Test Result -----------------------
            Assert.IsTrue(uiFormInfo.Status.IsNew);
            Assert.IsTrue(uiFormInfo.Status.IsDeleted);
            Assert.IsTrue(uiTab.Status.IsDeleted);
            Assert.IsTrue(uiColumnLayout.Status.IsDeleted);
            Assert.IsTrue(uiFieldInfo1.Status.IsDeleted);
            Assert.IsTrue(uiFieldInfo2.Status.IsDeleted);
        }

        [Test]
        public void Test_UIFields_NoTabs_ShouldReturnNoFields()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsShared.CreateSavedUIFormInfo();
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, uiFormInfo.UITabs.Count);
            //---------------Execute Test ----------------------
            ReadOnlyCollection<UIField> fields = uiFormInfo.UIFields;
            //---------------Test Result -----------------------
            Assert.AreEqual(0, fields.Count);
        }

        [Test]
        public void Test_UIFields_NoColumnLayouts_ShouldReturnNoFields()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsShared.CreateSavedUIFormInfo();
            UITab uiTab = TestUtilsShared.CreateSavedUITabInfo(uiFormInfo);
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, uiFormInfo.UITabs.Count);
            Assert.AreEqual(0, uiTab.UIColumnLayouts.Count);
            //---------------Execute Test ----------------------
            ReadOnlyCollection<UIField> fields = uiFormInfo.UIFields;
            //---------------Test Result -----------------------
            Assert.AreEqual(0, fields.Count);
        }

        [Test]
        public void Test_UIFields_NoFields_ShouldReturnNoFields()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsShared.CreateSavedUIFormInfo();
            UITab uiTab = TestUtilsShared.CreateSavedUITabInfo(uiFormInfo);
            UIColumnLayout uiColumnLayout = TestUtilsShared.CreateSavedUIColumnLayoutInfo(uiTab);
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, uiFormInfo.UITabs.Count);
            Assert.AreEqual(1, uiTab.UIColumnLayouts.Count);
            Assert.AreEqual(0, uiColumnLayout.UIFields.Count);
            //---------------Execute Test ----------------------
            ReadOnlyCollection<UIField> fields = uiFormInfo.UIFields;
            //---------------Test Result -----------------------
            Assert.AreEqual(0, fields.Count);
        }

        [Test]
        public void Test_UIFields_TwoFields_OnOneColumn_ShouldReturnTwoFields()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsShared.CreateSavedUIFormInfo();
            UITab uiTab = TestUtilsShared.CreateSavedUITabInfo(uiFormInfo);
            UIColumnLayout uiColumnLayout = TestUtilsShared.CreateSavedUIColumnLayoutInfo(uiTab);
            TestUtilsShared.CreateSavedUIField(uiColumnLayout);
            TestUtilsShared.CreateSavedUIField(uiColumnLayout);
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, uiFormInfo.UITabs.Count);
            Assert.AreEqual(1, uiTab.UIColumnLayouts.Count);
            Assert.AreEqual(2, uiColumnLayout.UIFields.Count);
            //---------------Execute Test ----------------------
            ReadOnlyCollection<UIField> fields = uiFormInfo.UIFields;
            //---------------Test Result -----------------------
            Assert.AreEqual(2, fields.Count);
        }

        [Test]
        public void Test_UIFields_5Fields_On3Column_On2Tabs_ShouldReturn5Fields()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsShared.CreateSavedUIFormInfo();
            UITab uiTab = TestUtilsShared.CreateSavedUITabInfo(uiFormInfo);
            UIColumnLayout uiColumnLayout = TestUtilsShared.CreateSavedUIColumnLayoutInfo(uiTab);
            TestUtilsShared.CreateSavedUIField(uiColumnLayout);
            TestUtilsShared.CreateSavedUIField(uiColumnLayout);
            UIColumnLayout uiColumnLayout1 = TestUtilsShared.CreateSavedUIColumnLayoutInfo(uiTab);
            TestUtilsShared.CreateSavedUIField(uiColumnLayout1);

            UITab uiTab2 = TestUtilsShared.CreateSavedUITabInfo(uiFormInfo);
            UIColumnLayout uiColumnLayout2 = TestUtilsShared.CreateSavedUIColumnLayoutInfo(uiTab2);
            TestUtilsShared.CreateSavedUIField(uiColumnLayout2);
            TestUtilsShared.CreateSavedUIField(uiColumnLayout2);
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, uiFormInfo.UITabs.Count);
            Assert.AreEqual(2, uiTab.UIColumnLayouts.Count);
            Assert.AreEqual(1, uiTab2.UIColumnLayouts.Count);
            Assert.AreEqual(2, uiColumnLayout.UIFields.Count);
            Assert.AreEqual(1, uiColumnLayout1.UIFields.Count);
            Assert.AreEqual(2, uiColumnLayout2.UIFields.Count);
            //---------------Execute Test ----------------------
            ReadOnlyCollection<UIField> fields = uiFormInfo.UIFields;
            //---------------Test Result -----------------------
            Assert.AreEqual(5, fields.Count);
        }

        [Test]
        public void Test_IUIForm_Height_Get()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsShared.CreateUnsavedValidUIFormInfo();
            IUIForm uiForm = uiFormInfo;

            //---------------Assert Precondition----------------
            Assert.Greater(uiFormInfo.Height, 0);
            //---------------Execute Test ----------------------
            int height = uiForm.Height;
            //---------------Test Result -----------------------
            Assert.AreEqual(uiFormInfo.Height, height);
        }

        [Test]
        public void Test_IUIForm_Height_Set()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsShared.CreateUnsavedValidUIFormInfo();
            IUIForm uiForm = uiFormInfo;
            int newHeight = TestUtilsShared.GetRandomInt();

            //---------------Assert Precondition----------------
            Assert.AreNotEqual(newHeight, uiFormInfo.Height);
            //---------------Execute Test ----------------------
            uiForm.Height = newHeight;
            //---------------Test Result -----------------------
            Assert.AreEqual(newHeight, uiForm.Height);
        }

        [Test]
        public void Test_IUIForm_Width_Get()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsShared.CreateUnsavedValidUIFormInfo();
            IUIForm uiForm = uiFormInfo;

            //---------------Assert Precondition----------------
            Assert.Greater(uiFormInfo.Width, 0);
            //---------------Execute Test ----------------------
            int width = uiForm.Width;
            //---------------Test Result -----------------------
            Assert.AreEqual(uiFormInfo.Width, width);
        }

        [Test]
        public void Test_IUIForm_Width_Set()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsShared.CreateUnsavedValidUIFormInfo();
            IUIForm uiForm = uiFormInfo;
            int newWidth = TestUtilsShared.GetRandomInt();

            //---------------Assert Precondition----------------
            Assert.AreNotEqual(newWidth, uiFormInfo.Width);
            //---------------Execute Test ----------------------
            uiForm.Width = newWidth;
            //---------------Test Result -----------------------
            Assert.AreEqual(newWidth, uiForm.Width);
        }

        [Test]
        public void Test_IUIForm_Title_Get()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsShared.CreateUnsavedValidUIFormInfo();
            uiFormInfo.Title = TestUtilsShared.GetRandomString();
            IUIForm uiForm = uiFormInfo;

            //---------------Assert Precondition----------------
            Assert.Greater(uiFormInfo.Title.Length, 0);
            //---------------Execute Test ----------------------
            string title = uiForm.Title;
            //---------------Test Result -----------------------
            Assert.AreEqual(uiFormInfo.Title, title);
        }

        [Test]
        public void Test_IUIForm_Title_Set()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsShared.CreateUnsavedValidUIFormInfo();
            IUIForm uiForm = uiFormInfo;
            string newTitle = TestUtilsShared.GetRandomString();

            //---------------Assert Precondition----------------
            Assert.AreNotEqual(newTitle, uiFormInfo.Title);
            //---------------Execute Test ----------------------
            uiForm.Title = newTitle;
            //---------------Test Result -----------------------
            Assert.AreEqual(newTitle, uiForm.Title);
        }


        [Test]
        public void Test_ICollection_Count()
        {
            //---------------Set up test pack-------------------
            UITab tabInfo = TestUtilsShared.CreateUnsavedValidUITabInfo();
            UIFormInfo uiFormInfo = tabInfo.UIFormInfo;
            IUIForm uiForm = uiFormInfo;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(uiFormInfo);
            Assert.AreEqual(1, uiFormInfo.UITabs.Count);
            //---------------Execute Test ----------------------
            int count = uiForm.Count;

            //---------------Test Result -----------------------
            Assert.AreEqual(uiFormInfo.UITabs.Count, count);
        }

        [Test]
        public void Test_IUIForm_Contains_ReturnsTrue()
        {
            //---------------Set up test pack-------------------
            UITab uiTab = TestUtilsShared.CreateUnsavedValidUITabInfo();
            UIFormInfo uiFormInfo = uiTab.UIFormInfo;
            IUIForm uiForm = uiFormInfo;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(uiFormInfo);
            Assert.IsTrue(uiFormInfo.UITabs.Contains(uiTab));
            //---------------Execute Test ----------------------
            bool contains = uiForm.Contains(uiTab);

            //---------------Test Result -----------------------
            Assert.IsTrue(contains); ;
        }

        [Test]
        public void Test_IUIForm_Contains_ReturnsFalse()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = TestUtilsShared.CreateUnsavedValidUIFormInfo();
            IUIForm uiForm = uiFormInfo;
            UITab tab = new UITab();
            //---------------Assert Precondition----------------
            Assert.IsFalse(uiFormInfo.UITabs.Contains(tab));
            //---------------Execute Test ----------------------
            bool contains = uiForm.Contains(tab);

            //---------------Test Result -----------------------
            Assert.IsFalse(contains);
        }

        [Test]
        public void Test_IUIForm_this()
        {
            //---------------Set up test pack-------------------
            UITab tabInfo1 = TestUtilsShared.CreateUnsavedValidUITabInfo();
            UIFormInfo uiFormInfo = tabInfo1.UIFormInfo;
            UITab tabInfo2 = TestUtilsShared.CreateUnsavedValidUITabInfo(uiFormInfo);
            IUIForm uiForm = uiFormInfo;

            //---------------Assert Precondition----------------
            Assert.AreEqual(2, uiFormInfo.UITabs.Count);
            //---------------Execute Test ----------------------
            IUIFormTab formTab = uiForm[1];
            //---------------Test Result -----------------------
            Assert.IsNotNull(formTab);
            Assert.AreSame(tabInfo2, formTab);
        }

        [Test]
        public void Test_CreateNewUITabInfos_ShouldSetOrdinals()
        {
            //---------------Set up test pack-------------------
            UIFormInfo uiFormInfo = new UIFormInfo();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            UITab uiTab1 = uiFormInfo.UITabs.CreateBusinessObject();
            UITab uiTab2 = uiFormInfo.UITabs.CreateBusinessObject();
            UITab uiTab3 = uiFormInfo.UITabs.CreateBusinessObject();
            UITab uiTab4 = uiFormInfo.UITabs.CreateBusinessObject();
            UITab uiTab5 = uiFormInfo.UITabs.CreateBusinessObject();
            UITab uiTab6 = uiFormInfo.UITabs.CreateBusinessObject();
            //---------------Test Result -----------------------
            TestUtilsShared.AssertItemsAreInOrder(uiTab1, uiTab2, uiTab3, uiTab4, uiTab5, uiTab6);
        }
        [Test]
        public void Test_GetParent()
        {
            //---------------Set up test pack-------------------
            UIView view = new UIView();
            UIFormInfo formInfo = new UIFormInfo();
            view.UIFormInfo = formInfo;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IModelElement parent = ((IModelElement)formInfo).Parent;
            //---------------Test Result -----------------------
            Assert.AreSame(view, parent);
        }

        [Test]
        public void Test_SetParentExceptionThrown()
        {
            //---------------Set up test pack-------------------
            IModelElement prop = new UIFormInfo();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            Exception exceptionThrown = null;
            try
            {
                prop.Parent = new UIView();
            }
            catch (Exception ex)
            {
                exceptionThrown = ex;
            }
            //---------------Test Result -----------------------
            Assert.IsNotNull(exceptionThrown);
            Assert.IsInstanceOf(typeof(HabaneroDeveloperException), exceptionThrown);
            StringAssert.Contains
                ("Set the parent of the UIFormInfo by setting the related UIView object", exceptionThrown.Message);
        }
 }
}